/*
** Mortem's HGE Addon 1.4
** Copyright (C) 2006-2007, Osokin 'elmortem' Makar
** http://elmortem.mirgames.ru
**
** HGE based GUI controls.
*/

#ifndef _MHAGUICTRLS_H_
#define _MHAGUICTRLS_H_

#include <hge.h>
#include <hgegui.h>
#include <hgefont.h>
#include <hgesprite.h>
#include <mhacallback.h>

#define mhaGetCheckCtrl(gui,id) ((mhaGuiCheck*)gui->GetCtrl(id))
#define mhaGetListboxCtrl(gui,id)		((mhaGuiListbox*)gui->GetCtrl(id))
#define mhaGetTextButtonCtrl(gui,id)		((mhaGuiTextButton*)gui->GetCtrl(id))
#define mhaGetInputCtrl(gui,id) ((mhaGuiInput*)gui->GetCtrl(id))
#define mhaGetTextCtrl(gui,id) ((mhaGuiText*)gui->GetCtrl(id))
#define mhaGetScrollCtrl(gui,id) ((mhaGuiScroll*)gui->GetCtrl(id))

#define BUTTON_LEFT 0
#define BUTTON_RIGHT 1
#define BUTTON_CENTER 2

enum {
	MHAGUI_OVER = 0,
	MHAGUI_CLICK
};

/*
** mhaGuiWindow (Picture)
*/

class mhaGuiWindow : public hgeGUIObject {
public:
	mhaGuiWindow(int id, int shadow, float x, float y, float tx, float ty, float tw, float th, HTEXTURE tex, bool center = true);
	~mhaGuiWindow(void);

	virtual void Render(void);

protected:
	static HGE *hge;
	int shadow;
	float x, y;
	hgeSprite		*spr;
};

/*
** mhaGuiWindow2 (Picture from file)
*/

class mhaGuiWindow2 : public hgeGUIObject {
public:
	mhaGuiWindow2(int id, float x, float y, const HGE_CHAR *filename, bool center = false);
	~mhaGuiWindow2(void);

	virtual void Render(void);

protected:
	static HGE *hge;
	float x, y;
	HTEXTURE tex;
	hgeSprite		*spr;
};

/*
** mhaGuiCheck
*/

class mhaGuiCheck : public hgeGUIObject {
public:
	mhaGuiCheck(int id, hgeFont *font, float fntscale, int fntcolor, float x, float y, float tx, float ty, float tw, float th, HTEXTURE  tex, const HGE_CHAR *text, mhaCallback *callback = 0, bool check = false);
	~mhaGuiCheck(void);

	virtual void Render(void);
  virtual bool MouseLButton(bool bDown);
	bool GetCheck(void);
	void SetCheck(bool check);

protected:
	hgeFont *font;
	float fntScale;
	int fntColor;
	float x, y, tx, ty;
	hgeSprite		*sprNoCheck, *sprCheck;
	HGE_CHAR *text;
	bool isCheck;
	mhaCallback *Callback;
};

/*
** mhaGuiButton
*/

class mhaGuiButton : public hgeGUIObject {
public:
	mhaGuiButton(int id, float x, float y, float tx, float ty, float tw, float th, HTEXTURE  tex);
	~mhaGuiButton(void);

	virtual void Render(void);
	virtual void MouseOver(bool bOver);
  virtual bool MouseLButton(bool bDown); 

protected:
	float x, y;
	hgeSprite		*sprNormal, *sprOver;
	bool isOver;
};

/*
** mhaGuiTextButton
*/

class mhaGuiTextButton : public hgeGUIObject {
public:
	mhaGuiTextButton(int id, hgeFont *font, int shadow, float fntscale, int _fntcolornormal, int _fntcolorover, float x, float y, float tx, float ty, float tw, float th, float allwidth, int align, HTEXTURE  tex, const HGE_CHAR *text, mhaCallback *callback = 0);
	~mhaGuiTextButton(void);

	virtual void Render(void);
	virtual void MouseOver(bool bOver);
  virtual bool MouseLButton(bool bDown); 
  virtual void Update(float fDeltaTime); 
	virtual void SetText(const HGE_CHAR *text);
	virtual HGE_CHAR *GetText(void);

protected:
	float x, y, w, h, tx, tw;
	int align;
	hgeFont *font;
	int shadow;
	float fntScale;
	int fntColorNormal, fntColorOver;
	HGE_CHAR *text;
	hgeSprite	*sprNormalLeft, *sprNormalCenter, *sprNormalRight;
	hgeSprite *sprOverLeft, *sprOverCenter, *sprOverRight;
	bool isOver;
	bool isSprite;
	mhaCallback *Callback;
};

/*
** mhaGuiListbox
*/
struct mhaGuiListboxItem
{
	HGE_CHAR *text;
	void *userdata;
	mhaGuiListboxItem *next;
};

class mhaGuiListbox : public hgeGUIObject
{
public:
	mhaGuiListbox(int id, float x, float y, float w, float h, hgeFont *fnt, float fntscale, DWORD tColor, DWORD thColor, DWORD hColor, bool selected = true);
	virtual			~mhaGuiListbox();

	int				AddItem(HGE_CHAR *item, void *userdata = 0);
	void			DeleteItem(int n);
	int				GetSelectedItem() { return nSelectedItem; }
	void			SetSelectedItem(int n);
	int				GetTopItem() { return nTopItem; }
	void			SetTopItem(int n) { if(n>=0 && n<=GetNumItems()-GetNumRows()) nTopItem=n; }

	HGE_CHAR			*GetItemText(int n);
	int				GetNumItems() { return nItems; }
	int				GetNumRows() { font->SetScale(fntScale) ;return int((rect.y2-rect.y1)/font->GetHeight()*font->GetScale()); }
	void			Clear();

	virtual void	Render();
	virtual bool	MouseMove(float x, float y) { mx=x; my=y; return false; }
	virtual bool	MouseLButton(bool bDown);
	virtual bool	MouseWheel(int nNotches);
	virtual bool	KeyClick(int key, int chr);

private:
	hgeSprite		*sprHighlight;
	hgeFont			*font;
	float fntScale;
	DWORD			textColor, texthilColor;

	int					nItems, nSelectedItem, nTopItem;
	float				mx, my;
	bool bSelected;
	mhaGuiListboxItem	*pItems;
};

/*
** mhaGuiInput
*/

class mhaGuiInput : public hgeGUIObject{
public: 
   mhaGuiInput(int id, hgeFont* fnt, float size, int shadow, float x, float y, float width, int color, int max, const HGE_CHAR* prevalue); 
   ~mhaGuiInput(); 
   virtual void Render(); 
   virtual bool MouseLButton(bool bDown); 
   virtual void Focus(bool bFocused); 
   virtual bool KeyClick(int key, int chr); 
   virtual void Update(float fDeltaTime); 
   void SetText(const HGE_CHAR *text); 
   HGE_CHAR *GetText(); 
	 void SetPos(float x, float y);

protected: 
   int caretposition; 
   HGE_CHAR* caret; 
   bool caretVisible; //for blinking effect 
   bool focused; 
   float caretTimer; //for blinking effect 
   int max; 
   float x; 
   float y; 
   hgeFont *font; 
	 float fntScale;
	 int shadow;
	 int color;
   float width; 
   HGE_CHAR *content; 
};

/*
** mhaGuiScroll
*/

class mhaGuiScroll : public hgeGUIObject {
public:
	mhaGuiScroll(int id, float x, float y, float size, float tx, float ty, float tw, float th, HTEXTURE  tex, bool vertical);
	~mhaGuiScroll(void);

	virtual void Render(void);
  virtual bool MouseLButton(bool bDown);
	virtual bool MouseMove(float x, float y);

	void SetMode(float min, float max);
	void SetValue(float value);
	float GetValue(void);

protected:
	float x, y, w, h, tw, th, size, pos;
	bool isPressed, isVertical;
	hgeSprite	*sprBegin, *sprCenter, *sprEnd, *sprButton;

	float min, max, value;
};

class mhaGuiText : public hgeGUIObject {
public:
	mhaGuiText(int id, hgeFont *font, float size, int color, int shadow, float x, float y, float width, float height, int align, const HGE_CHAR *text);
	~mhaGuiText(void);

	virtual void Render(void);

	void SetText(const HGE_CHAR *text);

protected:
	hgeFont *font;
	float fontSize;
	int fontColor;
	int fontAlign;
	int shadow;
	float x, y, width, height;
	HGE_CHAR *text;
};

#endif
